package de.koller.pulsar.scene.star;

import de.koller.aframe.util.math.IFunction;
import de.koller.aframe.util.misc.ColorOperations;
import de.koller.oceanFramework.math.RVec2;
import java.awt.Point;
import java.util.Iterator;

public class FuzzyStar {
	
	private final IFunction DISPOSE = new DisposeFunction();
	public int color = 0xff_ffffff;
	public final RVec2 position = new RVec2();
	public float radius = 1;
	
	public int render( float x, float y ) {
		float d = (float)Point.distance( position.x, position.y, x, y );
		if( d >= radius )
			return 0xff_000000;
		
		float k = DISPOSE.getValue( 1f / radius * d );
		return ColorOperations.multWithoutAlpha( color, k );
	}

	public Iterator<RVec2> getRenderIterator( float stepSize ) {
		return new RenderRangeIterator( stepSize );
	}
	
	private class RenderRangeIterator implements Iterator<RVec2> {

		private final float stepSize;
		private float x;
		private float y;
		private float maxX;
		private float maxY;
		
		public RenderRangeIterator( float stepSize ) {
			this.stepSize = stepSize;
			
			x = position.x - radius - 1;
			y = position.y - radius - 1;
			maxX = position.x + radius + 1;
			maxY = position.y + radius + 1;
		}
		
		@Override
		public boolean hasNext() {
			return y < maxY;
		}

		@Override
		public RVec2 next() {
			RVec2 p = new RVec2( x, y );
			
			x += stepSize;
			if( x > maxX ) {
				x = position.x - radius - 1;
				y += stepSize;
			}
			
			return p;
		}

		@Override public void remove() {}
		
	}
	
	private static class DisposeFunction implements IFunction {

		@Override
		public float getValue( float x ) {
			float f = (float)Math.exp( -x*6 );
			return f;
		}
		
	}
	
}
